//
//  RootViewController.m
//  WangI
//
//  Created by 陈曦 on 15/11/23.
//  Copyright © 2015年 WJN. All rights reserved.
//

#import "RootViewController.h"
#import "OneCollectionViewCell.h"
@interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) UIView *daohang;
//导航button
@property (nonatomic, retain) UIButton *buttonToutiao;
@property (nonatomic, retain) UIButton *buttonYule;
@property (nonatomic, retain) UIButton *buttonRedian;
@property (nonatomic, retain) UIButton *buttonTiyu;
@property (nonatomic, retain) UIButton *buttonCaijing;
//model
@property (nonatomic, retain) NSMutableArray *shuju;
@end
/*
 
 接口：头条
 GET http://c.3g.163.com/nc/article/headline/T1348647909107/0-20.html?from=toutiao&size=20&passport=&devId=Y3FQMlJW9VRIubYRjzlceQ%3D%3D&lat=&lon=&version=5.4.1&net=wifi&ts=1448249211&sign=TWpHrrWO6FS1c%2BJ2RW2j4nRhik4RDuCxTK3WPu2xWAl48ErR02zJ6%2FKXOnxX046I&encryption=1
 
 娱乐
 GET http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html
 
 热点
 http://c.3g.163.com/recommend/getSubDocPic?from=&size=20&passport=&devId=Y3FQMlJW9VRIubYRjzlceQ%3D%3D&lat=&lon=&version=5.4.1&net=wifi&ts=1448250922&sign=%2FPJsqTzg4hefI6BsEyd1wRKDqRRvOQev3ihB8s47p%2FZ48ErR02zJ6%2FKXOnxX046I&encryption=1
 
 体育
 http://c.3g.163.com/nc/article/list/T1348649079062/0-20.html
 
 财经
 GET http://c.3g.163.com/nc/article/list/T1348648756099/0-20.html
 
 
 
 
 */
@implementation RootViewController
-(void)dealloc {
    [_daohang release];
    [_collection release];
    [_shuju release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
//    self.shuju = [[NSMutableArray alloc] init];
    [_shuju release];
    NSMutableArray *array1 = [[NSMutableArray alloc] init];
    [WebAsk chuan:Ktoutiao withBlock:^(id result) {
        NSArray *array = [result valueForKey:@"T1348647909107"];
        for (NSDictionary *dic in array) {
            ToutiaoModel *toutiaomodel = [[ToutiaoModel alloc] initWithDic:dic];
            [array1 addObject:toutiaomodel];
        }
        _shuju = array1;
        [_collection reloadData];
    }];
    
    [self.view addSubview:self.daohang];
    [_daohang release];
    [self.view addSubview:self.collection];
    [_collection release];
}

#pragma mark -- collection协议
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    OneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OneCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.item == 0) {
        OneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OneCollectionViewCell" forIndexPath:indexPath];
        cell.wxzm = _shuju;
        return cell;
    } else if (indexPath.item == 1) {
        OneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OneCollectionViewCell" forIndexPath:indexPath];
        cell.wxzm = _shuju;
        return cell;
    } else if (indexPath.item == 2) {
        OneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OneCollectionViewCell" forIndexPath:indexPath];
        cell.wxzm = _shuju;
        return cell;
    } else if (indexPath.item == 3) {
        OneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OneCollectionViewCell" forIndexPath:indexPath];
        cell.wxzm = _shuju;
        return cell;
    } else {
        OneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OneCollectionViewCell" forIndexPath:indexPath];
        cell.wxzm = _shuju;
        return cell;
    }
    
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
        NSMutableArray *array1 = [[NSMutableArray alloc] init];
        [WebAsk chuan:Ktoutiao withBlock:^(id result) {
            NSArray *array = [result valueForKey:@"T1348647909107"];
            for (NSDictionary *dic in array) {
                ToutiaoModel *toutiaomodel = [[ToutiaoModel alloc] initWithDic:dic];
                [array1 addObject:toutiaomodel];
            }
            _shuju = array1;
            [_collection reloadData];
        }];
        for (int i = 0; i < 5; i++) {
            UIButton *button = (UIButton *)[self.daohang viewWithTag:100 + i];
            button.tintColor = [UIColor grayColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        _buttonToutiao.tintColor = [UIColor redColor];
        _buttonToutiao.titleLabel.font = [UIFont systemFontOfSize:18];
    } else if (scrollView.contentOffset.x == self.view.bounds.size.width) {
        NSMutableArray *array1 = [[NSMutableArray alloc] init];
        [WebAsk chuan:Kyule withBlock:^(id result) {
            NSArray *array = [result valueForKey:@"T1348648517839"];
            for (NSDictionary *dic in array) {
                ToutiaoModel *toutiaomodel = [[ToutiaoModel alloc] initWithDic:dic];
                [array1 addObject:toutiaomodel];
            }
            _shuju = array1;
            NSLog(@"%ld", _shuju.count);
            [_collection reloadData];
        }];
        for (int i = 0; i < 5; i++) {
            UIButton *button = (UIButton *)[self.daohang viewWithTag:100 + i];
            button.tintColor = [UIColor grayColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        _buttonYule.tintColor = [UIColor redColor];
        _buttonYule.titleLabel.font = [UIFont systemFontOfSize:18];
    } else if (scrollView.contentOffset.x == self.view.bounds.size.width * 2) {
        NSMutableArray *array1 = [[NSMutableArray alloc] init];
        [WebAsk chuan:Kredian withBlock:^(id result) {
            NSArray *array = [result valueForKey:@"T1348649580692"];
            for (NSDictionary *dic in array) {
                ToutiaoModel *toutiaomodel = [[ToutiaoModel alloc] initWithDic:dic];
                [array1 addObject:toutiaomodel];
            }
            _shuju = array1;
            [_collection reloadData];
        }];
        for (int i = 0; i < 5; i++) {
            UIButton *button = (UIButton *)[self.daohang viewWithTag:100 + i];
            button.tintColor = [UIColor grayColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        _buttonRedian.tintColor = [UIColor redColor];
        _buttonRedian.titleLabel.font = [UIFont systemFontOfSize:18];
    } else if (scrollView.contentOffset.x == self.view.bounds.size.width * 3) {
        NSMutableArray *array1 = [[NSMutableArray alloc] init];
        [WebAsk chuan:Ktiyu withBlock:^(id result) {
            NSArray *array = [result valueForKey:@"T1348649079062"];
            for (NSDictionary *dic in array) {
                ToutiaoModel *toutiaomodel = [[ToutiaoModel alloc] initWithDic:dic];
                [array1 addObject:toutiaomodel];
            }
            _shuju = array1;
            [_collection reloadData];
        }];
        for (int i = 0; i < 5; i++) {
            UIButton *button = (UIButton *)[self.daohang viewWithTag:100 + i];
            button.tintColor = [UIColor grayColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        _buttonTiyu.tintColor = [UIColor redColor];
        _buttonTiyu.titleLabel.font = [UIFont systemFontOfSize:18];
    } else if (scrollView.contentOffset.x == self.view.bounds.size.width * 4) {
        NSMutableArray *array1 = [[NSMutableArray alloc] init];
        [WebAsk chuan:Kcaijing withBlock:^(id result) {
            NSArray *array = [result valueForKey:@"T1348648756099"];
            for (NSDictionary *dic in array) {
                ToutiaoModel *toutiaomodel = [[ToutiaoModel alloc] initWithDic:dic];
                [array1 addObject:toutiaomodel];
            }
            _shuju = array1;
            [_collection reloadData];
        }];
        for (int i = 0; i < 5; i++) {
            UIButton *button = (UIButton *)[self.daohang viewWithTag:100 + i];
            button.tintColor = [UIColor grayColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        _buttonCaijing.tintColor = [UIColor redColor];
        _buttonCaijing.titleLabel.font = [UIFont systemFontOfSize:18];
    }
}

#pragma mark -- view控件
-(UICollectionView *)collection {
    if (!_collection) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height - 152);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 108, self.view.bounds.size.width, self.view.bounds.size.height - 152) collectionViewLayout:layout];
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.pagingEnabled = YES;
        [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        [_collection registerClass:[OneCollectionViewCell class] forCellWithReuseIdentifier:@"OneCollectionViewCell"];
        [_collection release];
    }
    return _collection;
}
-(UIView *)daohang {
    if (!_daohang) {
        self.daohang = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 44)];
        [_daohang release];
        
        self.buttonToutiao = [UIButton buttonWithType:UIButtonTypeSystem];
        _buttonToutiao.frame = CGRectMake(0, 0, self.view.bounds.size.width / 5, 44);
        [_buttonToutiao release];
        _buttonToutiao.tintColor = [UIColor redColor];
        [_buttonToutiao setTitle:@"头条" forState:UIControlStateNormal];
        _buttonToutiao.titleLabel.font = [UIFont systemFontOfSize:18];
        _buttonToutiao.tag = 100;
        [_buttonToutiao addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
        [_daohang addSubview:_buttonToutiao];
        
        self.buttonYule = [UIButton buttonWithType:UIButtonTypeSystem];
        _buttonYule.frame = CGRectMake(self.view.bounds.size.width / 5, 0, self.view.bounds.size.width / 5, 44);
        [_buttonYule release];
        _buttonYule.tintColor = [UIColor grayColor];
        [_buttonYule setTitle:@"娱乐" forState:UIControlStateNormal];
        _buttonYule.tag = 101;
        [_buttonYule addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
        [_daohang addSubview:_buttonYule];
        
        self.buttonRedian = [UIButton buttonWithType:UIButtonTypeSystem];
        _buttonRedian.frame = CGRectMake(self.view.bounds.size.width / 5 * 2, 0, self.view.bounds.size.width / 5, 44);
        [_buttonRedian release];
        _buttonRedian.tintColor = [UIColor grayColor];
        [_buttonRedian setTitle:@"热点" forState:UIControlStateNormal];
        _buttonRedian.tag = 102;
        [_buttonRedian addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
        [_daohang addSubview:_buttonRedian];
        
        self.buttonTiyu = [UIButton buttonWithType:UIButtonTypeSystem];
        _buttonTiyu.frame = CGRectMake(self.view.bounds.size.width / 5 * 3, 0, self.view.bounds.size.width / 5, 44);
        [_buttonTiyu release];
        _buttonTiyu.tintColor = [UIColor grayColor];
        [_buttonTiyu setTitle:@"体育" forState:UIControlStateNormal];
        _buttonTiyu.tag = 103;
        [_buttonTiyu addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
        [_daohang addSubview:_buttonTiyu];
        
        self.buttonCaijing = [UIButton buttonWithType:UIButtonTypeSystem];
        _buttonCaijing.frame = CGRectMake(self.view.bounds.size.width / 5 * 4, 0, self.view.bounds.size.width / 5, 44);
        [_buttonCaijing release];
        _buttonCaijing.tintColor = [UIColor grayColor];
        [_buttonCaijing setTitle:@"财经" forState:UIControlStateNormal];
        _buttonCaijing.tag = 104;
        [_buttonCaijing addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
        [_daohang addSubview:_buttonCaijing];
    }
    return _daohang;
}

#pragma mark -- button

-(void)button:(UIButton *)sender {
    if (sender.tag == 100) {
        NSMutableArray *array1 = [[NSMutableArray alloc] init];
        [WebAsk chuan:Ktoutiao withBlock:^(id result) {
            NSArray *array = [result valueForKey:@"T1348647909107"];
            for (NSDictionary *dic in array) {
                ToutiaoModel *toutiaomodel = [[ToutiaoModel alloc] initWithDic:dic];
                [array1 addObject:toutiaomodel];
            }
            _shuju = array1;
            [_collection reloadData];
        }];
        [_collection setContentOffset:CGPointMake(0, 0) animated:YES];
        for (int i = 0; i < 5; i++) {
            UIButton *button = (UIButton *)[self.daohang viewWithTag:100 + i];
            button.tintColor = [UIColor grayColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        _buttonToutiao.tintColor = [UIColor redColor];
        _buttonToutiao.titleLabel.font = [UIFont systemFontOfSize:18];

    } else if (sender.tag == 101) {
        NSMutableArray *array1 = [[NSMutableArray alloc] init];
        [WebAsk chuan:Kyule withBlock:^(id result) {
            NSArray *array = [result valueForKey:@"T1348648517839"];
            for (NSDictionary *dic in array) {
                ToutiaoModel *toutiaomodel = [[ToutiaoModel alloc] initWithDic:dic];
                [array1 addObject:toutiaomodel];
            }
            _shuju = array1;
            NSLog(@"%ld", _shuju.count);
            [_collection reloadData];
        }];
        [_collection setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:YES];
        for (int i = 0; i < 5; i++) {
            UIButton *button = (UIButton *)[self.daohang viewWithTag:100 + i];
            button.tintColor = [UIColor grayColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        _buttonYule.tintColor = [UIColor redColor];
        _buttonYule.titleLabel.font = [UIFont systemFontOfSize:18];
    } else if (sender.tag == 102) {
        [_collection setContentOffset:CGPointMake(self.view.bounds.size.width * 2, 0) animated:YES];
        for (int i = 0; i < 5; i++) {
            UIButton *button = (UIButton *)[self.daohang viewWithTag:100 + i];
            button.tintColor = [UIColor grayColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        _buttonRedian.tintColor = [UIColor redColor];
        _buttonRedian.titleLabel.font = [UIFont systemFontOfSize:18];
    } else if (sender.tag == 103) {
        [_collection setContentOffset:CGPointMake(self.view.bounds.size.width * 3, 0) animated:YES];
        for (int i = 0; i < 5; i++) {
            UIButton *button = (UIButton *)[self.daohang viewWithTag:100 + i];
            button.tintColor = [UIColor grayColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        _buttonTiyu.tintColor = [UIColor redColor];
        _buttonTiyu.titleLabel.font = [UIFont systemFontOfSize:18];
    } else if (sender.tag == 104) {
        [_collection setContentOffset:CGPointMake(self.view.bounds.size.width * 4, 0) animated:YES];
        for (int i = 0; i < 5; i++) {
            UIButton *button = (UIButton *)[self.daohang viewWithTag:100 + i];
            button.tintColor = [UIColor grayColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        _buttonCaijing.tintColor = [UIColor redColor];
        _buttonCaijing.titleLabel.font = [UIFont systemFontOfSize:18];
    }
}















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
